package poker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

class deckException extends Exception {
	deckException(final String w) {
		super(w);
	}
}

public class deck {
	private ArrayList<card> talia;

	deck() throws deckException {
		talia = new ArrayList<card>();
		for (int i = 2; i <= 10; i++) {
			try {
				talia.add(new card(Integer.toString(i), 'S'));
				talia.add(new card(Integer.toString(i), 'H'));
				talia.add(new card(Integer.toString(i), 'D'));
				talia.add(new card(Integer.toString(i), 'C'));
			} catch (cardException e) {
				// TODO Auto-generated catch block
				throw new deckException(e.getMessage());
			} catch (OutOfMemoryError e) {
				// TODO Auto-generated catch block
				throw new deckException(e.getMessage());
			}
		}
		String figury = "JQKA";
		for (int i = 0; i < 4; i++) {
			try {
				talia.add(new card("" + figury.charAt(i), 'S'));
				talia.add(new card("" + figury.charAt(i), 'H'));
				talia.add(new card("" + figury.charAt(i), 'D'));
				talia.add(new card("" + figury.charAt(i), 'C'));
			} catch (cardException e) {
				// TODO Auto-generated catch block
				throw new deckException(e.getMessage());
			} catch (OutOfMemoryError e) {
				// TODO Auto-generated catch block
				throw new deckException(e.getMessage());
			}
		}
	}

	public final void shuffle() throws deckException {
		if (talia == null || talia.isEmpty()) {
			throw new deckException("nie ma tali");
		}
		Collections.shuffle(talia, new Random());
	}

	public final synchronized card takeCard() throws deckException {
		if (talia == null || talia.isEmpty()) {
			throw new deckException("nie ma tali");
		}
		card temp = talia.get(talia.size() - 1);
		talia.remove(talia.size() - 1);
		return temp;
	}
}
